<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class register extends CI_Controller {
		
	// load register view
	function index(){
		$this->load->view('register_step1');
	}
	
	// check if input email address exists in TB_user
	function emailExists(){
		$this->load->model("register_model");    //get database ready
		
		$inputEmail = $this-> input->get_post('email')  ; 	  //email from passed in parameter
		if (!isset($inputEmail)) { //missing parameter email
			echo 'Missing parameter: email';
		}
		else {   //got email parameter
			$str =  $this->register_model->checkEmail($inputEmail);   //check email
			echo $str ;    //return string true or false
		}
		
	}
	
	// check if input username exists in TB_User
	function usernameExists(){
        $this->load->model("register_model");    //get database ready
		
        $inputUsername = $this-> input->get_post('username')  ; 	  //username from passed in parameter
		if (!isset($inputUsername)) { //missing parameter username
			echo 'Missing parameter: username';
		}
		else {   //got email parameter
			$str = $this->register_model->checkUsername($inputUsername);   //check username
			echo $str ;    //return string true or false
		}
	}
	
	//check if validationcode is available
	function iscodeValid(){
		$inputCode = $this -> input -> get_post('code');    //get code passed from parameter
		if (!isset($inputCode)){  //missing parameter
			echo 'false';
		}
		else {
			$this -> load -> model("register_model");
			if ($this->register_model -> isCodeValid($inputCode)) 
				echo 'true';
			else
				echo 'false';
		}
	}
    
	function SessionClean(){
		$this ->session ->unset_userdata('colinyan@gmail.com');
		echo 'session[colinyan@hotmail.com] has been cleaned.';
	}
	
	
	//reset activation emails
	function resendActivationEmail(){
		$Email = $this -> input -> get_post('email');
		//$Email = 'colinyan@hotmail.com';
		$this -> load -> model("register_model");
		echo $this -> register_model ->resendValidationEmail($Email); 
	}
	
	//change registed email address
	function changeEmail(){
		$oldEmail = $this -> input -> get_post('oldemail');
		$newEmail = $this -> input -> get_post('newemail');
		
		//$oldEmail = 'colinyan@hotmail.com';
		//$newEmail = 'colinyan@gmail.com';
		
		if (!isset($oldEmail) || !isset($newEmail)) { echo 'error'; return; }    //input variables are invalid
		
		$this->load->model("register_model");    //get database ready
		
		if ($this->register_model->checkEmail($oldEmail)=='false'){   //old email dose not exists in database
			echo 'nouser'; return; 
		}
		if ($this->register_model->checkEmail($newEmail)=='true'){   //new email already exists in database
			echo 'used'; return; 
		}
		else {  //new email is available to exchange
			if ($this -> register_model -> exchangeEmail($oldEmail, $newEmail)){  //exchange email successfully
				echo $this -> register_model ->resendValidationEmail($newEmail);  //resend validation email
			}
			else {
				echo 'fail';    //fail to update database
			}
		}
	}
	
	//active user's account
	function activation(){
		$code = $this -> input ->get_post('code');   //get activation code from GET method
		
		$this->load->model('register_model');
		$result = $this -> register_model -> activeUserAccount($code);   //get user data from database
        if ($result == 'success'){
            $row = $this -> register_model -> getUserdataByCode($code);  //load user's information
        	if (isset($row)){  //got user information
              $data['reg_username'] = $row-> user_name;
        	  $data['reg_email'] = $row -> email;
        	  $this -> load -> view('register_step3', $data);   //load step3 page
        	}
        	else {   //fail to locate user
        		echo "Fail to connect databse, please goto login page.";
        	}
        }
        elseif ($result == 'active'){  //Already activated
        	$this -> load -> view('login_view');
        }
	}
	
	// process submitted form
	function step2() {	
		$Email = $this->input ->get_post('regEmail');
		$Username = $this ->input ->get_post('regNickName');
		$Password = $this ->input ->get_post('regPassword');
		$Telphone = $this ->input -> get_post('regTelphone');
		$Address = $this->input->get_post('regAddress');

		if (!IsValidEmail(trim($Email))){
			ErrorMsg("Email不合法.");
		}
		elseif (strlen(trim($Username))<=0){
			ErrorMsg("用户名不可以为空");
		}
		else {
  		  $this->load->model("register_model");    //get database ready
		  if ($this->register_model -> checkEmail($inputEmail)=='true'){  //check if current user exists
			  ErrorMsg("用户已经存在!");
		  }
		  else {
   		    if ($this -> register_model -> insertNewUser($Email, $Username, $Password, $Telphone, $Address)){    //insert data into database
   			    $mydata['reg_email'] =  $Email;   //pass email as a parameter
			    $this->load->view('register_step2', $mydata);
		    }	
		    else{
			    ErrorMsg("用户增加失败, 请按浏览器的返回键重试.");
		    }	
		  }
		}
	}
		
	
}